package com.fg.exemplocrudengenhariadesoftware.daobd;

import java.sql.Connection;
import java.sql.PreparedStatement;
import org.apache.log4j.Logger;

import com.fg.exemplocrudengenhariadesoftware.model.Aluno;

public class AlunoDao {
	private JDBCUitl JDBCUitl;
	private Connection conexaoBD;
	Logger logger;
	
	public AlunoDao() {
		JDBCUitl = new JDBCUitl();
		logger = Logger.getLogger(this.getClass().getName());
	}
	
	public void insere (Aluno aluno) throws ErroAoConectarBDException {
		PreparedStatement prep = null;
		String stringSQL = "";
		
		try {
			conexaoBD = JDBCUitl.getConexaoBd();
			if (conexaoBD != null) {
				stringSQL = "INSERT INTO TBALUNO (MATRICULA, NOME, SENHA) VALUES (?, ?, ?)";
				logger.debug("insert:" + stringSQL);
				try {
					prep = conexaoBD.prepareStatement(stringSQL);
					prep.setInt(1, aluno.getMatricula());
					prep.setString(2, aluno.getNome());
					prep.setString(3, aluno.getSenha());
					prep.executeUpdate();
				} catch (Exception e1) {
					logger.error("erro ao inserir Aluno:" + aluno.toString());
				}
			}
		} catch (Exception e) {
			throw new ErroAoConectarBDException();
		}	
	}
	public  String update() throws ErroAoConectarBDException {
		PreparedStatement prep = null;
		String stringSQL = "";
        
        try {
			conexaoBD = JDBCUitl.getConexaoBd();
			if (conexaoBD != null) {
				stringSQL = "UPDATE TBALUNO SET NOME=?,SENHA=? WHERE MATRIULA=?";
				logger.debug("insert:" + stringSQL);
        
        try
        {
            Class.forName("com.mysql.jdbc.Driver");
            prep = conexaoBD.prepareStatement(stringSQL);
            prep.setString(1, "nome");
            prep.setString(2, "senha");
            prep.setString(3, "matricula");
            prep.executeUpdate();
            prep.close();

        } 
        catch (ClassNotFoundException e)
        {
            e.printStackTrace();

        }
        return "Update Feito com sucesso!";
    }
			
       } catch (Exception e) {
			throw new ErroAoConectarBDException();
		}
		return stringSQL;
        	
	}  
	public void delete (Aluno aluno) throws ErroAoConectarBDException {
		PreparedStatement prep = null;
		String stringSQL = "";
		
		try {
			conexaoBD = JDBCUitl.getConexaoBd();
			if (conexaoBD != null) {
				stringSQL = "DELETE FROM TBALUNO WHERE MATRICULA=?";
				logger.debug("delete:" + stringSQL);
				try {
					prep = conexaoBD.prepareStatement(stringSQL);
					prep.setInt(1, aluno.getMatricula()); /*
					prep.setString(2, aluno.getNome());
					prep.setString(3, aluno.getSenha());*/
					prep.executeUpdate();
				} catch (Exception e1) {
					logger.error("erro ao deletar Aluno:" + aluno.toString());
				}
			}
		} catch (Exception e) {
			throw new ErroAoConectarBDException();
		}	
	}
}